import re
import nltk
from phrase_chunker import chunkPhrase, extractNounPhrase, extractVerbPhrase

modalAdj = "(advisable|best|better|certain|convenient|desirable|difficult|disappointing|easier|easiest|easy|economical|good|likely|necessary|possible|useful)"
cogVed = "(anticipated|assumed|believed|expected|known|recommended|thought)"

pattern1a = re.compile(r"(It|it) (is|was) " + modalAdj + " (that) ")
pattern1b = re.compile(r"(It|it) (is|was) (not|more|most) " + modalAdj + " (that) ")
pattern2a = re.compile(r"(It|it) (is|was) " + modalAdj + " (to) ")
pattern2b = re.compile(r"(It|it) (is|was) (not|more|most) " + modalAdj + " (to) ")
pattern2c = re.compile(r"(It|it) (is|was) " + modalAdj + " (for) ")
pattern2d = re.compile(r"(It|it) (is|was) (not|more|most) " + modalAdj + " (for) ")
pattern3a = re.compile(r"(It|it) (is|was) " + cogVed + " (that) ")
pattern4a = re.compile(r"(It|it) (seems|appears|means|follows) (that) ")
pattern5a = re.compile(r" (makes|finds) (it) " + modalAdj + " (to) ")
pattern5b = re.compile(r" (makes|finds) (it) (not|more|most) " + modalAdj + " (to) ")
pattern5c = re.compile(r" (makes|finds) (it) " + modalAdj + " (for) ")
pattern5d = re.compile(r" (makes|finds) (it) (not|more|most) " + modalAdj + " (for) ")
pattern6a = re.compile(r"(It|it) (is|was) (time) (to) ")
pattern7a = re.compile(r"(It|it) (is|was) (thanks) (to) ")

NPpattern = {}
NPpattern[0] = pattern1a
NPpattern[1] = pattern1b
NPpattern[2] = pattern2c
NPpattern[3] = pattern2d
NPpattern[4] = pattern3a
NPpattern[5] = pattern4a
NPpattern[6] = pattern7a

VPpattern = {}
VPpattern[0] = pattern2a
VPpattern[1] = pattern2b
VPpattern[2] = pattern6a

NNpattern = {}
NNpattern[0] = pattern5c
NNpattern[1] = pattern5d

NVpattern = {}
NVpattern[0] = pattern5a
NVpattern[1] = pattern5b

def tagSentence(sentence):
	words = nltk.word_tokenize(str(sentence))
	tags = nltk.pos_tag(words)
	return tags

# def syntacticPattern(line):
def isPleonastic(line):
	# pleonasticList = []
	# pronounList = []
	# lines = open("train/refres/train/it-girl.txt", 'r').readlines()
	# for line in lines:
	# line = line.strip()
	# if "It" in line or "it" in line:
	result = None
	j = 0
	while result == None and j < len(NPpattern):
		result = NPpattern[j].search(line)
		if result != None:
			phrases = extractNounPhrase(chunkPhrase(tagSentence(line)))
			aftermatch = line[result.end():]
			i = 0
			NP = None
			while i < len(phrases):
				if re.match(phrases[i], aftermatch):
					NP = phrases[i]
					break
				i += 1
			if NP != None:
				# pleonasticList.append(result.string[result.start():result.end()] + NP)
				return True
		j += 1
	j = 0
	while result == None and j < len(VPpattern):
		result = VPpattern[j].search(line)
		if result != None:
			phrases = extractVerbPhrase(chunkPhrase(tagSentence(line)))
			aftermatch = line[result.end():]
			i = 0
			VP = None
			while i < len(phrases):
				if re.match(phrases[i], aftermatch):
					VP = phrases[i]
					break
				i += 1
			if VP != None:
				# pleonasticList.append(result.string[result.start():result.end()] + VP)
				return True
		j += 1
	j = 0
	while result == None and j < len(NNpattern):
		result = NNpattern[j].search(line)
		if result != None:
			parsetree = chunkPhrase(tagSentence(line))
			nounphrases = extractNounPhrase(parsetree)
			beforematch = line[:result.start()]
			aftermatch = line[result.end():]
			i = 0
			NP = None
			while i < len(nounphrases):
				if re.match(nounphrases[i], beforematch):
					NP = nounphrases[i]
					break
				i += 1
			i = 0
			NP2 = None
			while i < len(nounphrases):
				if re.match(nounphrases[i], aftermatch):
					NP2 = nounphrases[i]
					break
				i += 1
			if NP != None and NP2 != None:
				# pleonasticList.append(NP + result.string[result.start():result.end()] + NP2)
				return True
		j += 1
	j = 0
	while result == None and j < len(NVpattern):
		result = NVpattern[j].search(line)
		if result != None:
			parsetree = chunkPhrase(tagSentence(line))
			nounphrases = extractNounPhrase(parsetree)
			verbphrases = extractVerbPhrase(parsetree)
			beforematch = line[:result.start()]
			aftermatch = line[result.end():]
			i = 0
			NP = None
			while i < len(nounphrases):
				if re.match(nounphrases[i], beforematch):
					NP = nounphrases[i]
					break
				i += 1
			i = 0
			VP = None
			while i < len(verbphrases):
				if re.match(verbphrases[i], aftermatch):
					VP = verbphrases[i]
					break
				i += 1
			if NP != None and VP != None:
				# pleonasticList.append(NP + result.string[result.start():result.end()] + VP)
				return True
		j += 1
	if result == None:
		# pronounList.append(line)
		return False
	
	# print "PRONOUN:"
	# for pron in pronounList:
		# print pron
	# print ""
	# print "PLEONASTIC:"
	# for pleo in pleonasticList:
		# print pleo

def test():
	result = pattern1a.search("It was good that he")
	print result.group()
	print result.start()
	print result.end()
	print result.string[result.end():]
	
# syntacticPattern()
# test()
